package org.java.order.web;

import org.java.common.entity.Cart;
import org.java.order.aspect.NoRepeatSubmit;
import org.java.order.aspect.WebLog;
import org.java.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * @author WHQ
 * @version 1.0
 * @date 2022-3-15 0015 下午 3:57
 */
@RestController
public class OrderController {
    @Autowired
    OrderService orderService;

    /**
     * 访问地址:http://localhost:21000/create
     * 访问地址:http://api.shopping.com/order/create
     * @return
     */
//    @NoRepeatSubmit
    @GetMapping("/create")
    @WebLog(description = "")
    public String createOrder(){
        orderService.createOrder();
        return "插入成功!";
    }

    /**
     * 访问地址:http://localhost:21000/submit
     * http://api.shopping.com/order/submit
     * 提交订单的方法
     * @param
     * @param
     * @return
     */
//    @NoRepeatSubmit
    @PostMapping("/submit")
    @WebLog(description = "提交订单信息")
    public ResponseEntity<Void> submit(@RequestBody Cart[] carts){
        System.out.println("进入方法");
        orderService.submit(carts);
        return ResponseEntity.ok().build();
    }
    @GetMapping("/confirm")
    @WebLog(description = "得到订单信息")
//    @NoRepeatSubmit
    public ResponseEntity<List<Cart>> confirmOrder(){
        //确认订单的方法
        List<Cart> list=orderService.confirmOrder();
        return ResponseEntity.ok().body(list);
    }

    /**
     * 生成订单号，并且创建订单的方法
     * 访问地址:http:api.shopping.com/order/getOrderTradeAndGenerateOrder
     * @param carts
     * @return
     */
    @WebLog(description = "生成订单信息")
    @PostMapping("/getOrderTradeAndGenerateOrder")
//    @NoRepeatSubmit(timeout = 20)
    public ResponseEntity<String> getOrderTradeAndGenerateOrder(@RequestBody Cart[] carts){
        for (Cart cart : carts) {
            System.out.println(cart.getSkuId());
        }
        //生成订单号
        String tradeNum = UUID.randomUUID().toString();
        //生成订单
        orderService.generateOrder(tradeNum,carts);
        //返回订单号
        return ResponseEntity.ok().body(tradeNum);
    }

    /**
     * 订单支付完成以后，调用的修改订单状态接口
     * 访问地址:http://api.shopping.com/order/modifyOrder
     * @param tradeNum
     * @return
     */
    @NoRepeatSubmit
    @PostMapping("/modifyOrder/{tradeNum}")
    @WebLog(description = "修改订单信息")
    public ResponseEntity<Void> modifyOrder(@PathVariable("tradeNum") String tradeNum){
        System.out.println("tradeNum:"+tradeNum);
        orderService.modifyOrder(tradeNum);
        return ResponseEntity.ok().build();
    }
}
